﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace YArchitech.Controls.ImgListView
{
	internal class ImageListViewActionLists : DesignerActionList, IServiceProvider, IWindowsFormsEditorService, ITypeDescriptorContext
	{
		public ImageListViewActionLists(IComponent component) : base(component)
		{
			this.imageListView = (ImageListView)component;
			this.designerService = (DesignerActionUIService)base.GetService(typeof(DesignerActionUIService));
		}

		private void SetProperty(string propName, object value)
		{
			PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(this.imageListView)[propName];
			if (propertyDescriptor == null)
			{
				throw new ArgumentException("Unknown property.", propName);
			}
			propertyDescriptor.SetValue(this.imageListView, value);
		}

		public ColumnType SortColumn
		{
			get
			{
				return this.imageListView.SortColumn;
			}
			set
			{
				this.SetProperty("SortColumn", value);
			}
		}

		public SortOrder SortOrder
		{
			get
			{
				return this.imageListView.SortOrder;
			}
			set
			{
				this.SetProperty("SortOrder", value);
			}
		}

		public Autodesk.Revit.DB.View View
		{
			get
			{
				return this.imageListView.View;
			}
			set
			{
				this.SetProperty("View", value);
			}
		}

		public void EditColumns()
		{
			this.property = TypeDescriptor.GetProperties(this.imageListView)["Columns"];
			UITypeEditor uitypeEditor = (UITypeEditor)this.property.GetEditor(typeof(UITypeEditor));
			object value = this.imageListView.Columns;
			value = uitypeEditor.EditValue(this, this, value);
			this.SetProperty("Columns", value);
			this.designerService.Refresh(base.Component);
		}

		public override DesignerActionItemCollection GetSortedActionItems()
		{
			return new DesignerActionItemCollection
			{
				new DesignerActionMethodItem(this, "EditColumns", "Edit Columns", true),
				new DesignerActionPropertyItem("View", "View"),
				new DesignerActionPropertyItem("SortColumn", "SortColumn"),
				new DesignerActionPropertyItem("SortOrder", "SortOrder")
			};
		}

		object IServiceProvider.GetService(Type serviceType)
		{
			if (serviceType.Equals(typeof(IWindowsFormsEditorService)))
			{
				return this;
			}
			return base.GetService(serviceType);
		}

		void IWindowsFormsEditorService.CloseDropDown()
		{
			throw new NotSupportedException("Only modal dialogs are supported.");
		}

		void IWindowsFormsEditorService.DropDownControl(System.Windows.Forms.Control control)
		{
			throw new NotSupportedException("Only modal dialogs are supported.");
		}

		DialogResult IWindowsFormsEditorService.ShowDialog(System.Windows.Forms.Form dialog)
		{
			return dialog.ShowDialog();
		}

		IContainer ITypeDescriptorContext.Container
		{
			get
			{
				return null;
			}
		}

		object ITypeDescriptorContext.Instance
		{
			get
			{
				return this.imageListView;
			}
		}

		void ITypeDescriptorContext.OnComponentChanged()
		{
		}

		bool ITypeDescriptorContext.OnComponentChanging()
		{
			return true;
		}

		PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor
		{
			get
			{
				return this.property;
			}
		}

		private ImageListView imageListView;

		private DesignerActionUIService designerService;

		private PropertyDescriptor property;
	}
}
